﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Discuz.Common;


namespace TestDiscuz
{
    public partial class IDictionary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Collection collec = new Collection(4);
            collec[0] = 1;
            collec[1] = "str";
            collec[2] = new object();
            collec[3] = collec;
            string s = "";
            IEnumerator enumrator = collec.GetEnumerator();
            while (enumrator.MoveNext())
            {
                s += enumrator.Current.ToString();
            }
            Response.Write(s);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Write(HashHelpers.GetPrime(100));
        }
    }

    public class Collection : System.Collections.ICollection
    {
        private object[] ObjArr;
        private string msg = default(string);

        public Collection(int count)
        {
            ObjArr = new object[count];
        }
        public Collection()
            : this(1000)
        {
        }

        public object this[int index]
        {
            get {
                try
                {
                    return ObjArr[index];
                }
                catch(Exception e)
                {
                    msg = e.Message;
                }
                return null;
            }

            set {                
                try
                {
                    ObjArr.SetValue(value, index);
                }
                catch (Exception e)
                {
                    msg = e.Message;
                }
            }

        }

        public override string ToString()
        {
            return "TestDiscuz.IDictionary";
        }
        
        #region ICollection 成员

        public void CopyTo(Array array, int index)
        {
            try
            {
                foreach (object o in ObjArr)
                {
                    ObjArr.SetValue(o, index++);
                }
            }
            catch (Exception e)
            {
                msg = e.Message;
            }
        }

        public int Count
        {
            get { return ObjArr.Length; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return this; }
        }

        #endregion

        #region IEnumerable 成员

        public IEnumerator GetEnumerator()
        {
            return new CollectionSimple(this);
        }
        #endregion

        private class CollectionSimple : IEnumerator
        {
            private Collection _instance;
            private object currentElement;
            private int index;

            public CollectionSimple(Collection instance)
            {
                _instance = instance;
                this.index = -1;
            }

            #region IEnumerator 成员

            public object Current
            {
                get
                {
                    if (this.index == -1)
                    {
                        throw new InvalidOperationException(("InvalidOperation_EnumNotStarted"));
                    }
                    if (this.index >= this._instance.Count)
                    {
                        throw new InvalidOperationException(("InvalidOperation_EnumEnded"));
                    }
                    return this.currentElement;
                }
            }

            public bool MoveNext()
            {
                if (this.index < (this._instance.Count - 1))
                {
                    this.index++;
                    this.currentElement = this._instance[this.index];
                    return true;
                }
                this.index = this._instance.Count;
                return false;

            }

            public void Reset()
            {
                this.index = -1;
            }

            #endregion
        }

    }

    public class Dictionary : System.Collections.IDictionary
    {
        private struct KeyValueCollection
        {
            private object key;
            private object value;
            public KeyValueCollection(object k, object v)
            {
                key = k;
                value = v;
            }
            public object Key
            {
                get { return key; }
                set { key = value; }
            }
            public object Value
            {
                get { return this.value; }
                set { this.value = value; }
            }
        }

        private System.Collections.Generic.List<KeyValueCollection> List = new List<KeyValueCollection>();

        #region IDictionary 成员

        public void Add(object key, object value)
        {
            this.List.Add(new KeyValueCollection(key,value));
        }

        public void Clear()
        {
            this.List.Clear();
        }

        public bool Contains(object key)
        {
            foreach (KeyValueCollection kv in this.List)
            {
                if (key.Equals(kv.Key))
                {
                    return true;
                }
            }
            return false;
        }

        public IDictionaryEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public ICollection Keys
        {
            get
            {
                if (this.List.Count > 0)
                {
                    object[] keys = new object[this.List.Count];
                    int n = 0;
                    foreach (KeyValueCollection v in this.List)
                    {
                        keys[n++] = v.Key;
                    }
                    return keys;
                }
                return null;
            }
        }

        public void Remove(object key)
        {
            throw new NotImplementedException();
        }

        public ICollection Values
        {
            get
            {
                if (this.List.Count > 0)
                {
                    object[] keys = new object[this.List.Count];
                    int n = 0;
                    foreach (KeyValueCollection v in this.List)
                    {
                        keys[n++] = v.Value;
                    }
                    return keys;
                }
                return null;
            }
        }

        public object this[object key]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region ICollection 成员

        public void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsSynchronized
        {
            get { throw new NotImplementedException(); }
        }

        public object SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IEnumerable 成员

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }


}
